import { fetchAssetDetail } from '../services/api';

export default {
  namespace: 'assetDetail',
  state: {
    info: undefined,
    loading: false,
  },
  effects: {
    *fetchDetail({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(fetchAssetDetail, payload);

      yield put({
        type: 'loadDetail',
        payload: response,
      });

      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) {
        callback();
      }
    },
  },

  reducers: {
    changeLoading(state, { payload }) {
      return {
        ...state,
        loading: payload,
      };
    },
    loadDetail(state, { payload }) {
      return {
        ...state,
        info: payload,
      };
    },
  },
};

